<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px;width:300px;"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.message.value)"/>
        <textarea id="response" style="height: 300px;width:300px;"></textarea>
        <input type="button" value="清空" onclick="document.getElementById('response').value=''"/>
    </form>
    <script type="text/javascript">
        var socket;
        if (window.WebSocket) {
            socket = new WebSocket("ws://localhost:8080/hello")
            socket.onmessage = function(data) {
                var rt = document.getElementById("response")
                rt.value = rt.value + "\n" + data.data
            }

            socket.onopen = function (data) {
                console.log("连接开启")
                var rt = document.getElementById("response")
                rt.value = "连接开启"
            }

            socket.onclose = function (data) {
                var rt = document.getElementById("response")
                rt.value = rt.value + "\n连接关闭"
            }
        }
        function send(text) {
            if (!window.socket) {
                return;
            }
            if (socket.readyState == WebSocket.OPEN) {
                socket.send(text)
            }
        }
    </script>
</body>
</html>